package com.cntaiping.workutil.aimanager;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

@Aspect
@Component
public class AnnotationHandler {

    private static final Logger logger = LoggerFactory.getLogger(AnnotationHandler.class);

    @Around("@annotation(com.cntaiping.workutil.aimanager.LogTime)")
    public Object logAround(ProceedingJoinPoint joinPoint) throws  Throwable{

        StopWatch stopWatch = new StopWatch();
        MethodSignature ms = (MethodSignature) joinPoint.getSignature();
        LogTime logTime = ms.getMethod().getAnnotation(LogTime.class);
        stopWatch.start();
        Object proceed = joinPoint.proceed();
        stopWatch.stop();
        logger.info("{}执行{}ms",logTime.value(),stopWatch.getTotalTimeMillis());

        return proceed;
    }
}
